import pymysql
from setting import mysql_host


class SingleMysql:#mysql链接的单例模式，这个格式可以死记
    _instance = None
    _initialized = False

    def __new__(cls, *args, **kwargs):#去内存申请地址

        if not cls._instance:
            cls._instance = super().__new__(cls)
        return cls._instance

    def __init__(self, host = mysql_host):
        if not self._initialized:
            
            self.conn = pymysql.connect(
                host= host, 
                port=3306,
                user = 'root',
                password = '123456',
                charset = 'utf8' ,
                db = 'novel',
            )

            self.cursor = self.conn.cursor()

            self._initialized = True


 

class InsertBook:

    def __init__(self, book_info):

        single_mysql = SingleMysql()
        print(single_mysql)
        self.conn = single_mysql.conn
        self.cursor = single_mysql.cursor

        self.yongli = book_info

        self.insert_book_info()
        self.insert_chapter_info()

    def insert_book_info(self):

        sql = '''

        INSERT INTO book(
        book_name, book_url, book_status,

        book_intor, book_category, book_author,

        host) 

        VALUES

        (%s, %s, %s, %s, %s, %s, %s)

        '''

        self.cursor.execute(sql,
            (
                self.yongli['book_name'][0],
                self.yongli['book_url'],
                self.yongli['book_status'],
                self.yongli['book_intor'], 
                self.yongli['book_category'], 
                self.yongli['book_author'][0],
                self.yongli['host'],
            
            )
        )

        # self.conn.commit()
        # self.cursor.close()
        # self.conn.close()

    def insert_chapter_info(self):
        fetch_book_id = '''

            SELECT id FROM book WHERE book_url = %s;

        '''
        self.cursor.execute(fetch_book_id, (self.yongli['book_url'],))

        book_id = self.cursor.fetchone()[0]

        insert_chapter_info = ''' 

            INSERT INTO chapter(book_id, chapter_name, chapter_url) 
            VALUES
            (%s, %s, %s);

        '''

        for key, value in self.yongli['book_chapter'].items():

            self.cursor.execute(insert_chapter_info, (book_id, key, value))

        self.conn.commit()


class InsertChapter:

    def __init__(self, chapter_info):

        single_mysql = SingleMysql()
        print(single_mysql)
        self.conn = single_mysql.conn
        self.cursor = single_mysql.cursor
    
        self.yongli = chapter_info

        self.chapter_id = self.get_chapter_id()

        self.insert_chapter_content()

    def get_chapter_id(self):

        sql = '''

        SELECT id FROM chapter WHERE chapter_url=%s;


        '''

        self.cursor.execute(sql, self.yongli['chapter_url'])

        chapter_id = self.cursor.fetchone()[0]

        return chapter_id

    def insert_chapter_content(self):

        sql = '''

        INSERT INTO chapter_content VALUES (%s, %s);

        '''

        self.cursor.execute(sql, (self.chapter_id, self.yongli['chapter_content']))

        self.conn.commit()





if __name__ == '__main__':
    import yongli
    x = InsertBook(yongli.book_info)
    y = InsertChapter(yongli.chapter_info)

    




